<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>借书记录</title>
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/record.css">
    <script src="js/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div class="top">
       <span>网上借书系统</span>
       <div class="back">
           <a href="index.html">返回首页</a>
       </div>
    </div>
    <section>
        <div class="head-up">
            <div class="up">
                书名：<input type="text" id="book">
                作者名：<input type="text" id="autor">
                <button id="search">查询</button>
            </div>
            <div class="all" id="all"><a href="#">全部记录</a></div>
        </div>
        <div class="mid">
            <table >
                <thead>
                <tr>
                    <th>id</th>
                    <th>卡号</th>
                    <th>书名</th>
                    <th>作者</th>
                    <th>借阅时间</th>
                    <th>归还时间</th>
                    <th>归还截止时间</th>
                    <th>书籍类型</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>

                </thead>
                <tbody id="tab">

                </tbody>
            </table>
        </div>
    </section>
</body>
<script>



    $(document).ready(function (){
        function getBtn(){
            $(".return").click(function (){
                var tds = $(this).parent().parent().find("td:first").attr("class")
                console.log(tds)
                $.ajax({
                    type:"get",
                    url:"http://120.76.130.104:9000/user/returnBook",
                    data:{
                        id:$(this).parent().parent().find("td:first").attr("class")
                    },
                    headers:{
                        Token:localStorage.getItem("Token")
                    },
                    success:function (data,text,http){
                        if (http.getResponseHeader("Token") == "timeOut" || http.getResponseHeader("Token") == "false") {
                            alert(http.getResponseHeader("Token"))
                            localStorage.clear()
                            window.location = "login.html"
                        }else {
                            alert(data.msg)
                            $.ajax({
                                type:"get",
                                url:"http://120.76.130.104:9000/record/getRecord",
                                data:{},
                                headers:{
                                    Token:localStorage.getItem("Token")
                                },
                                success:function (data,text,http) {
                                    if (http.getResponseHeader("Token") == "timeOut" || http.getResponseHeader("Token") == "false") {
                                        alert(http.getResponseHeader("Token"))
                                        localStorage.clear()
                                        window.location = "login.html"
                                    } else {
                                        $("#tab").empty()
                                        for (let i = 0; i < data.data.length; i++) {
                                            $("#tab").append("    <tr>\n" +
                                                "                    <td class="+data.data[i].bookId+">" + data.data[i].id + "</td>\n" +
                                                "                    <td>" + data.data[i].scardId + "</td>\n" +
                                                "                    <td>" + data.data[i].bookName + "</td>\n" +
                                                "                    <td>" + data.data[i].author + "</td>\n" +
                                                "                    <td>" + data.data[i].startTime + "</td>\n" +
                                                "                    <td>" + data.data[i].time + "</td>\n" +
                                                "                    <td id='return'>" + data.data[i].returnTime + "</td>\n" +
                                                "                    <td>" + data.data[i].type + "</td>\n" +
                                                "                    <td>" + data.data[i].state + "</td>\n" +
                                                "                    <td><button class='return'>还书</button></td>\n" +
                                                "                </tr>")
                                        }

                                        getBtn()
                                        let tab = document.getElementById("tab")
                                        let trs = tab.getElementsByTagName("tr")


                                        for(let i = 0;i < trs.length; i++){
                                            if(i % 2 == 0){
                                                trs[i].style.backgroundColor = "#f5f5f5"
                                            }
                                        }
                                    }
                                }
                            })
                        }
                    }
                })
            })
            $('#tab tr').each(function() {

                if($(this).find('td').eq(5).html() != "借出中"){
                    $(this).find('td').eq(9).find("button").attr("disabled",true)
                    // console.log($(this).find('td').eq(9).find("button").html("已归还"))
                }
            });
        }
        $("#book,#autor").keydown(function (e){
            if(e.keyCode == 13){
                $.ajax({
                    type: "get",
                    url:"http://120.76.130.104:9000/record/getRecordByName",
                    data:{
                        name:$("#book").val(),
                        author:$("#autor").val()
                    },
                    headers:{
                        Token:localStorage.getItem("Token")
                    },
                    success:function (data,text,xmlHttp){
                        //判断Token是否过期
                        if (xmlHttp.getResponseHeader("Token") == "timeOut" || xmlHttp.getResponseHeader("Token") == "false") {
                            alert(xmlHttp.getResponseHeader("Token"))
                            localStorage.clear()
                            window.location = "login.html"
                        }else {
                            $("#tab").empty()
                            for (let i = 0; i < data.data.length; i++) {
                                $("#tab").append("    <tr>\n" +
                                    "                    <td class="+data.data[i].bookId+">" + data.data[i].id + "</td>\n" +
                                    "                    <td>" + data.data[i].scardId + "</td>\n" +
                                    "                    <td>" + data.data[i].bookName + "</td>\n" +
                                    "                    <td>" + data.data[i].author + "</td>\n" +
                                    "                    <td>" + data.data[i].startTime + "</td>\n" +
                                    "                    <td>" + data.data[i].time + "</td>\n" +
                                    "                    <td>" + data.data[i].returnTime + "</td>\n" +
                                    "                    <td>" + data.data[i].type + "</td>\n" +
                                    "                    <td>" + data.data[i].state + "</td>\n" +
                                    "                    <td><button class='return'>还书</button></td>\n" +
                                    "                </tr>")
                            }

                            getBtn();
                            let tab = document.getElementById("tab")
                            let trs = tab.getElementsByTagName("tr")

                            for(let i = 0;i < trs.length; i++){
                                if(i % 2 == 0){
                                    trs[i].style.backgroundColor = "#f5f5f5"
                                }
                            }
                        }
                    }
                })
            }
        })
        $("#all").click(function (){
            $.ajax({
                type:"get",
                url:"http://120.76.130.104:9000/record/getRecord",
                data:{},
                headers:{
                    Token:localStorage.getItem("Token")
                },
                success:function (data,text,http) {
                    if (http.getResponseHeader("Token") == "timeOut" || http.getResponseHeader("Token") == "false") {
                        alert(http.getResponseHeader("Token"))
                        localStorage.clear()
                        window.location = "login.html"
                    } else {
                        $("#tab").empty()
                        for (let i = 0; i < data.data.length; i++) {
                            $("#tab").append("    <tr>\n" +
                                "                    <td class="+data.data[i].bookId+">" + data.data[i].id + "</td>\n" +
                                "                    <td>" + data.data[i].scardId + "</td>\n" +
                                "                    <td>" + data.data[i].bookName + "</td>\n" +
                                "                    <td>" + data.data[i].author + "</td>\n" +
                                "                    <td>" + data.data[i].startTime + "</td>\n" +
                                "                    <td>" + data.data[i].time + "</td>\n" +
                                "                    <td id='return'>" + data.data[i].returnTime + "</td>\n" +
                                "                    <td>" + data.data[i].type + "</td>\n" +
                                "                    <td>" + data.data[i].state + "</td>\n" +
                                "                    <td><button class='return'>还书</button></td>\n" +
                                "                </tr>")
                        }

                        getBtn()
                        let tab = document.getElementById("tab")
                        let trs = tab.getElementsByTagName("tr")


                        for(let i = 0;i < trs.length; i++){
                            if(i % 2 == 0){
                                trs[i].style.backgroundColor = "#f5f5f5"
                            }
                        }
                    }
                }
            })
        })





        //判断Token是否为空
        if(localStorage.getItem("Token") == "" || localStorage.getItem("Token") == null){
            localStorage.clear()
            window.location = "login.html"
        }else{
            $("#search").click(function (){//根据书名和作者名查找数据
                $.ajax({
                    type: "get",
                    url:"http://120.76.130.104:9000/record/getRecordByName",
                    data:{
                        name:$("#book").val(),
                        author:$("#autor").val()
                    },
                    headers:{
                        Token:localStorage.getItem("Token")
                    },
                    success:function (data,text,xmlHttp){
                        //判断Token是否过期
                        if (xmlHttp.getResponseHeader("Token") == "timeOut" || xmlHttp.getResponseHeader("Token") == "false") {
                            alert(xmlHttp.getResponseHeader("Token"))
                            localStorage.clear()
                            window.location = "login.html"
                        }else {
                            $("#tab").empty()
                            for (let i = 0; i < data.data.length; i++) {
                                $("#tab").append("    <tr>\n" +
                                    "                    <td class="+data.data[i].bookId+">" + data.data[i].id + "</td>\n" +
                                    "                    <td>" + data.data[i].scardId + "</td>\n" +
                                    "                    <td>" + data.data[i].bookName + "</td>\n" +
                                    "                    <td>" + data.data[i].author + "</td>\n" +
                                    "                    <td>" + data.data[i].startTime + "</td>\n" +
                                    "                    <td>" + data.data[i].time + "</td>\n" +
                                    "                    <td>" + data.data[i].returnTime + "</td>\n" +
                                    "                    <td>" + data.data[i].type + "</td>\n" +
                                    "                    <td>" + data.data[i].state + "</td>\n" +
                                    "                    <td><button class='return'>还书</button></td>\n" +
                                    "                </tr>")
                            }

                            getBtn();
                            let tab = document.getElementById("tab")
                            let trs = tab.getElementsByTagName("tr")

                            for(let i = 0;i < trs.length; i++){
                                if(i % 2 == 0){
                                    trs[i].style.backgroundColor = "#f5f5f5"
                                }
                            }
                        }
                    }
                })
            })
            $.ajax({
                type:"get",
                url:"http://120.76.130.104:9000/record/getRecord",
                data:{},
                headers:{
                    Token:localStorage.getItem("Token")
                },
                success:function (data,text,http) {
                    if (http.getResponseHeader("Token") == "timeOut" || http.getResponseHeader("Token") == "false") {
                        alert(http.getResponseHeader("Token"))
                        localStorage.clear()
                        window.location = "login.html"
                    } else {

                        for (let i = 0; i < data.data.length; i++) {
                            $("#tab").append("    <tr>\n" +
                                "                    <td class="+data.data[i].bookId+">" + data.data[i].id + "</td>\n" +
                                "                    <td>" + data.data[i].scardId + "</td>\n" +
                                "                    <td>" + data.data[i].bookName + "</td>\n" +
                                "                    <td>" + data.data[i].author + "</td>\n" +
                                "                    <td>" + data.data[i].startTime + "</td>\n" +
                                "                    <td>" + data.data[i].time + "</td>\n" +
                                "                    <td >" + data.data[i].returnTime + "</td>\n" +
                                "                    <td>" + data.data[i].type + "</td>\n" +
                                "                    <td>" + data.data[i].state + "</td>\n" +
                                "                    <td><button class='return'>还书</button></td>\n" +
                                "                </tr>")
                        }

                        getBtn();
                        let tab = document.getElementById("tab")
                        let trs = tab.getElementsByTagName("tr")

                        for(let i = 0;i < trs.length; i++){
                            if(i % 2 == 0){
                                trs[i].style.backgroundColor = "#f5f5f5"
                            }
                        }
                    }
                }
            })
        }
    })
</script>
</html>